package jp.co.hcs.attendancemanagement.inputdailyattendance.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jp.co.hcs.attendancemanagement.AMConstants;
import jp.co.hcs.attendancemanagement.AMResourceKeyConstants;
import jp.co.hcs.attendancemanagement.AMResourceManager;
import jp.co.hcs.attendancemanagement.StrutsMessageConstants;
import jp.co.hcs.attendancemanagement.action.AMAction;
import jp.co.hcs.attendancemanagement.inputdailyattendance.actionform.InputDailyAttendanceForm;
import jp.co.hcs.attendancemanagement.inputdailyattendance.logic.InputDailyAttendanceLogic;
import jp.co.hcs.attendancemanagement.inputdailyattendance.logic.dto.EmployeeCompanyDTO;
import jp.co.hcs.attendancemanagement.util.DateUtil;
import jp.co.hcs.attendancemanagement.util.StringUtil;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.util.LabelValueBean;

/**
 * The Class InputDailyAttendanceInitAction.
 */
public class InputDailyAttendanceInitAction extends AMAction {

  /*
   * (non-Javadoc)
   * 
   * @see
   * jp.co.hcs.attendancemanagement.action.AMAction#executeImpl(org.apache.struts.action.ActionMapping
   * , org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest,
   * javax.servlet.http.HttpServletResponse)
   */
  public ActionForward executeImpl(ActionMapping mapping, ActionForm form,
      HttpServletRequest request, HttpServletResponse response) throws Exception {

    String userId = (String) request.getSession().getAttribute(AMConstants.USER_LOGGED_IN);

    ActionMessages errors = new ActionMessages();
    InputDailyAttendanceForm inputDailyAttendanceForm = (InputDailyAttendanceForm) form;
    inputDailyAttendanceForm.resetData();
    // init data
    inputDailyAttendanceForm.setDay(StringUtil.getSystemDate());
    inputDailyAttendanceForm.setMonth(StringUtil.getSystemMonth());
    inputDailyAttendanceForm.setYear(StringUtil.getSystemYear());
    inputDailyAttendanceForm.setDayOfWeek(StringUtil.dayNameOfWeekMap.get(DateUtil.dayOfWeekMap
        .get(StringUtil.getSystemDayOfWeek())));

    // get employee information
    InputDailyAttendanceLogic inputDailyAttendanceLogic = InputDailyAttendanceLogic.getInstance();
    EmployeeCompanyDTO initDTO = inputDailyAttendanceLogic.getEmployeeCompanyInfoDTO(userId);
    if (initDTO == null) {
      errors
          .add(ActionErrors.GLOBAL_MESSAGE, new ActionMessage(StrutsMessageConstants.ALARM_W1010));
    }
    inputDailyAttendanceForm.setInitDTO(initDTO);

    // get attendance classification list
    List<LabelValueBean> attendanceSettingList =
        inputDailyAttendanceLogic.getAttendanceSettingList();
    if (attendanceSettingList == null || attendanceSettingList.size() == 0) {
      errors.add(ActionErrors.GLOBAL_MESSAGE, new ActionMessage(
          StrutsMessageConstants.ALARM_W1051_ERROR, AMResourceManager
              .getMessage(AMResourceKeyConstants.FIELD_ATTENDANCECLASSIFICATION)));
    }
    inputDailyAttendanceForm.setAttendanceSettingList(attendanceSettingList);

    // get time classification list
    List<LabelValueBean> timeSettingList = inputDailyAttendanceLogic.getTimeSettingList();
    if (timeSettingList == null || timeSettingList.size() == 0) {
      errors.add(ActionErrors.GLOBAL_MESSAGE, new ActionMessage(
          StrutsMessageConstants.ALARM_W1051_ERROR, AMResourceManager
              .getMessage(AMResourceKeyConstants.FIELD_ATTENDANCECLASSIFICATION)));
    }
    inputDailyAttendanceForm.setTimeSettingList(timeSettingList);

    inputDailyAttendanceForm.setIsEditable(false);

    saveErrors(request, errors);

    return mapping.findForward(SUCCESS);
  }
}
